package com.soup.lab.executor.pool;

import java.util.*;
import java.util.function.Predicate;

/**
 * {@link Taggable} 可标记接口的默认实现
 * 不允许重复的标签，线程不安全
 */
public class TagHolder implements Taggable {

    private final Set<Tag> tags;

    public TagHolder() {
        tags = new LinkedHashSet<>();
    }

    @Override
    public void tag(Tag tag) {
        tags.add(tag);
    }

    @Override
    public List<Tag> getTags() {
        return new ArrayList<>(tags);
    }

    @Override
    public List<Tag> removeIf(Predicate<Tag> filter) {
        List<Tag> removeTags = new ArrayList<>();
        Iterator<Tag> it = tags.iterator();
        while (it.hasNext()) {
            Tag tag = it.next();
            if(filter.test(tag)) {
                removeTags.add(tag);
                it.remove();
            }
        }
        return removeTags;
    }

    @Override
    public void clear() {
        tags.clear();
    }
}
